package character.sprite.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import character.sprite.data.CharacterSpriteDatabase;
import character.sprite.model.CharacterSprite;

public class CharacterSpriteBuilder extends JDialog implements ActionListener,
		WindowListener {

	private static final long serialVersionUID = -5771875967789692858L;

	public static String spriteName;

	private CharacterStyleListPanel styleListPanel;
	private CharacterStyleDropZonePanel styleDropZonePanel;
	private CharacterSpriteAnimator spriteAnimator;
	private JButton importStyleButton, changeSpriteName;

	public CharacterSpriteBuilder(JFrame parent, String spriteName) {
		super(parent, "Editing Sprite");

		this.setSize(450, 400);
		this.setModal(true);
		this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		this.setLocation(this.getWidth(), this.getHeight());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLayout(new BorderLayout());

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		importStyleButton = new JButton("Import Styles");
		importStyleButton.addActionListener(this);
		changeSpriteName = new JButton("Change Sprite Name");
		changeSpriteName.addActionListener(this);
		buttonPanel.add(importStyleButton);
		buttonPanel.add(changeSpriteName);

		styleListPanel = new CharacterStyleListPanel();
		styleDropZonePanel = new CharacterStyleDropZonePanel();

		if (spriteName != null) {
			CharacterSpriteBuilder.spriteName = spriteName;
			this.setTitle("Editing Sprite: \"" + spriteName + "\"");
		} else {
			CharacterSprite sprite = new CharacterSprite("[[New Sprite]]");
			CharacterSpriteDatabase.getInstance().addNewSprite(sprite);
			CharacterSpriteBuilder.spriteName = sprite.getName();
			this.changeSpriteName();
		}

		spriteAnimator = new CharacterSpriteAnimator(225, 250);

		this.add(styleListPanel, BorderLayout.WEST);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(styleDropZonePanel, BorderLayout.NORTH);
		centerPanel.add(spriteAnimator, BorderLayout.CENTER);
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	private void changeSpriteName() {
		boolean done = false;
		String input = "[[New Sprite]]";
		while (!done) {
			input = JOptionPane.showInputDialog(this,
					"Enter a name for this sprite", "My Sprite",
					JOptionPane.INFORMATION_MESSAGE);
			if (input != null
					&& !CharacterSpriteDatabase.getInstance().spriteNameExists(
							input)) {
				done = true;
			}
		}
		CharacterSpriteDatabase.getInstance().changeSpriteName(spriteName,
				input);
		CharacterSpriteBuilder.spriteName = input;
		this.setTitle("Editing Sprite: \""
				+ CharacterSpriteBuilder.spriteName
				+ "\"");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JButton source = (JButton) arg0.getSource();
		if (source.equals(importStyleButton)) {
			CharacterStyleDeploymentTool importTool = new CharacterStyleDeploymentTool(
					this);
			importTool.setVisible(true);
		} else if (source.equals(changeSpriteName)) {
			this.changeSpriteName();
		}
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		spriteAnimator.stopAnimation();
		this.dispose();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
	}

}
